﻿using Bigger.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Bigger.Study.ConsoleApp
{
    public class TcpClientExample : IExample
    {
        public void Execute()
        {
            TcpClient objTcpClient = new TcpClient
            {
                NoDelay = true
            };
            IPEndPoint objIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9001);
            IAsyncResult objAsyncResult = objTcpClient.BeginConnect(objIPEndPoint.Address, objIPEndPoint.Port, null, null);
            bool success = objAsyncResult.AsyncWaitHandle.WaitOne(5000, false);
            if (!success)
            {
                try
                {
                    objTcpClient.Close();
                }
                catch
                {

                }
                throw new SocketException();
            }
            objTcpClient.EndConnect(objAsyncResult);

            ConsoleHelper.WriteLineYellow("连接成功");

            NetworkStream objNetworkStream = objTcpClient.GetStream();

            //StreamWriter objStreamWriter = new StreamWriter(objNetworkStream);
            BinaryWriter objBinaryWriter = new BinaryWriter(objNetworkStream);

            //objStreamWriter.Write("客户端发送信息：" + Guid.NewGuid() + "\r\n");
            //objStreamWriter.Flush();

            //objStreamWriter.Write("客户端发送信息：" + Guid.NewGuid() + "\r\n");
            //objStreamWriter.Flush();

            TestDemoContext objTestDemoContext = new TestDemoContext
            {
                S1 = "S1",
                I1 = 25,
                Url = "http://www.cnblogs.com/huangxincheng/archive/2013/01/18/2866492.html",
                Parameters = new Dictionary<string, object> 
                { 
                    {"S2","S2"},
                    {"I2",2},
                    {"Person",new Person{Name="郑廷建",Age=26}},
                },
            };

            string result = objTestDemoContext.NewJsonSerialize();

            //ConsoleHelper.WriteLineYellow("字节长度：" + bytes.Length);

            //objBinaryWriter.Write(bytes.Length);
            //objBinaryWriter.Write(true);
            //objBinaryWriter.Write(bytes);

            objBinaryWriter.Write(result);
            objBinaryWriter.Flush();


            return;


            StreamReader objStreamReader = new StreamReader(objNetworkStream);
            string readLine;
            while (!objStreamReader.EndOfStream)
            {
                readLine = objStreamReader.ReadLine();
                ConsoleHelper.WriteLineYellow("客户端接受信息：" + readLine);
            }
        }
    }

    [Serializable]
    public class TestDemoContext
    {
        public string S1 { get; set; }
        public int I1 { get; set; }
        public string Url { get; set; }
        public IDictionary<string, object> Parameters { get; set; }
    }

    [Serializable]
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
